<template>
    <div class="users" v-if="userInfo != null">
        <div class="users-bg">
            <img src="../assets/img/1e9da84b53ea4898ba859175609ac6bd.jpeg" alt="">
            <i @click="settingJump" class="iconfont">&#xe626;</i>
        </div>

        <div class="user-info">
            <input @change="submitAvatar(userInfo.email, $event)" type="file">
            <img v-lazy="userInfo.avatar == 'no' ? 'http://47.105.157.158:3000/images/upload/default.jpg' : `http://47.105.157.158:3000/images/upload/${userInfo.avatar}`">
        </div>

        <div class="user-name">{{userInfo.userName}}</div>

        <div class="user-fansh">
            <div class="fansh-item">
                <div>0</div>
                <div>声望</div>
            </div>
            <div class="fansh-item">
                <div>{{userInfo.fansh.length}}</div>
                <div>粉丝</div>
            </div>
            <div class="fansh-item">
                <div>{{userInfo.guanzhu.length}}</div>
                <div>关注</div>
            </div>
            <div class="fansh-item">
                <div>{{userInfo.beizan}}</div>
                <div>被赞</div>
            </div>

        </div>


        <div class="user-publish">
            <span @click="setTabIndex(true)" :class="tabIndex ? 'current-style' : ''">图片作品</span>
            <span @click="setTabIndex(false)" :class="tabIndex ? '' : 'current-style'">收藏</span>
        </div>


        <div class="tab-content" v-if="tabIndex == true">
            <div v-if="publishData === null || publishData.length === 0">
                您还没有发布过作品哦<br />
                请在桌面端发布作品
            </div>
            <div class="imglist" v-if="publishData != null && publishData.length != 0">
                <img 
                :key="index"
                v-for="(item, index) in publishData"
                v-lazy="`http://47.105.157.158:3000/images/cat/${item.cat}/${item.pathName}`">
            </div>  
        </div>

        <div class="tab-content" v-if="tabIndex == false">
            <div v-if="userInfo == null || userInfo.shoucang.length === 0">您还没有收藏哦</div>
            <div class="shoucang" v-if="userInfo != null && userInfo.shoucang.length != 0">
                <img :key="index" v-for="(item, index) in userInfo.shoucang" v-lazy="`http://47.105.157.158:3000/images/cat/${item.cat}/${item.pathName}`">
            </div>
        </div>


    </div>
</template>

<script>
import { ref, onMounted } from '@vue/composition-api';
import jquery from 'jquery';
import axios from 'axios';
export default {
    setup (props, context) {
        axios.defaults.baseURL = 'http://47.105.157.158:3000';

        onMounted(() => {
            getUserInfo();
        })

        // 用户信息
        const userInfo = ref(null);
        // 获取用户信息
        const getUserInfo = async function () {
            if (!window.localStorage.getItem('token')) {
                context.parent.$message.error('登录后，才能使用该功能');
                setTimeout(() => {
                    context.parent.$router.push('/login');
                }, 2000)
            } else {
                let { data: res } = await axios.get(`/user/login/${window.localStorage.getItem('token')}`);
                if (res.status != 200)
                    return context.parent.$message.error('获取用户信息失败，请尝试重新登录');
                userInfo.value = res.user;
                context.parent.$nextTick(() => {
                    getPublish();
                })
            }
        }

        // 发表的作品
        const publishData = ref(null);
        // 获取用户发表的作品
        const getPublish = async function () {
            let { data: res } = await axios.get(`/user/imgs/${userInfo.value.email}`);
            if (res.status != 200)
                return context.parent.$message.error('获取用户发表的作品失败，请重新尝试');
            publishData.value = res.imgs;
        }

        // 切换作品
        const tabIndex = ref(true);
        // 点击切换作品
        const setTabIndex = function (flag) {
            tabIndex.value = flag;
        }

        // 跳转到设置
        const settingJump = function () {
            context.parent.$router.push('/setting');
        }

        // 上传头像
        const submitAvatar = async function (email, e) {
            let formData = new FormData();
            formData.append('avatar', e.target.files[0]);
            formData.append('user', email);
            jquery.ajax({
                type: 'post',
                url: 'http://47.105.157.158:3000/upload/avatar',
                contentType: false,
                processData: false,
                data: formData,
                success: function (response) {
                     if (response.status != 200) {
                        context.parent.$message.error('上传头像失败，请重新尝试');
                    } else {
                        context.parent.$message.success('上传头像成功');
                        userInfo.value.avatar = response.avatar;
                    }
                } 
            });
        }

        const methods = {
            setTabIndex,
            settingJump,
            submitAvatar
            
        }

        return {
            ...methods,
            tabIndex,
            publishData,
            userInfo
        }
    }
}
</script>

<style lang="less" scoped>
.imglist,
.shoucang {
    columns: auto 4;
    column-gap: .266667rem;
    img {
        width: 100%;
    }
}
.users-bg {
    position: relative;
    height: 5.333333rem;
    i {
        position: absolute;
        right: .266667rem;
        bottom: .266667rem;
        font-size: 1.066667rem;
        color: #595959;
        font-weight: 700;
        cursor: pointer;
    }
}

.user-info {
    position: absolute;
    left: .213333rem;
    top: 5rem;
    width: 4rem;
    height: 4rem;
    input {
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        background-color: red;
        border-radius: 50%;
        // opacity: 0;
    }
    img {
        position: absolute;
        border-radius: 50%;
        left: 0;
        top: 0;
        pointer-events: none;
    }
}

.user-name {
    position: absolute;
    left: 4.8rem;
    top: 7.2rem;
    font-size: .88rem;
    color: #707070;
}

.user-fansh {
    position: absolute;
    top: 10rem;
    width: 100%;
    font-size: .666667rem;
    text-align: center;
    display: flex;
    .fansh-item {
        flex: 1;
    }
    .fansh-item div:nth-child(2) {
        color: #88888a;
    }
}

.user-publish {
    position: absolute;
    top: 13.333333rem;
    padding-left: .88rem;
    span {
        font-size: .88rem;
        color: #b0b0b0;
        font-weight: 700;
        margin-right: .666667rem;
    }
}

.current-style {
    color: #4b4b4b!important;
}

.tab-content {
    position: absolute;
    width: 100%;
    top: 16rem;
    text-align: center;
    color: #897272;
    font-size: .88rem;
    font-weight: 700;
}
</style>